﻿//Copyright © 2012  Daniel Lopez

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.WindowsAPICodePack.Shell;
namespace NASM_Develop_IDE
{
    public partial class NewProjectFromExisting : GlassForm
    {
        private Solution solution;
        private bool createdSuccesfully;
        private List<string> sourcesList;
        public NewProjectFromExisting()
        {
            InitializeComponent();
            sourcesList = new List<string>();
            TextBoxHighlighter.Highlight(projectPath, "Enter Project Path");
            TextBoxHighlighter.Highlight(solutionName, "Enter A Valid Solution Name");
            TextBoxHighlighter.Highlight(author, "Enter Name of the Author Of Project (Optionial)");
           // ActiveControl = sourcePanel;
            solution = null;
            createdSuccesfully = false;
            AeroGlassCompositionChanged += new EventHandler<AeroGlassCompositionChangedEventArgs>(NewProjectDlg_AeroGlassCompositionChanged);
            if (AeroGlassCompositionEnabled)
            {
                ExcludeControlFromAeroGlass(panel);
            }
            else
            {

                BackColor = Color.Aqua;
            }
        }
        void NewProjectDlg_AeroGlassCompositionChanged(object sender, AeroGlassCompositionChangedEventArgs e)
        {
            if (e.GlassAvailable)
            {

                ExcludeControlFromAeroGlass(panel);
                Invalidate();
            }
            else
            {

                this.BackColor = Color.Aqua;
            }
        }
        private void projectPath_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog folderDlg = new FolderBrowserDialog())
            {
                folderDlg.ShowDialog(this);
                folderDlg.RootFolder = Environment.SpecialFolder.MyDocuments;
                projectPath.Text = folderDlg.SelectedPath + "\\";
            }
        }
        public Solution ProjectSolution
        {
            get
            {
                return solution;
            }
        }
        public string Author
        {
            get
            {
                return author.Text;
            }
        }
        public bool SuccessfullyClosedWithOKAY
        {
            get
            {
                return createdSuccesfully;
            }
        }
        public string SolutionName
        {
            get
            {
                return solutionName.Text;
            }
        }
        private void okay_Click(object sender, EventArgs e)
        {

            string list = "";
            foreach (string source in sourcesList)
                list += "• " + source + "\n";
            DialogResult result = MessageBox.Show("The following source files listed below will be included in the project! Are you sure you want to continue? Click no to start all over again!\n\n" + list, "Sure you want to continue!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                if (!Directory.Exists(projectPath.Text + solutionName.Text) && sourcesList.Count > 0)
                {
                    try
                    {
                        DirectoryInfo dirInfo = Directory.CreateDirectory(projectPath.Text + solutionName.Text);
                        DirectoryInfo dirInfo2 = Directory.CreateDirectory(dirInfo.FullName + "\\bin");
                        string pathOfSolution = (dirInfo.Parent.FullName + "\\" + solutionName.Text + ".nasmproj");
                        Solution s = new Solution();
                        List<string> pathOfSources = new List<string>();
                        foreach (string source in sourcesList)
                        {
                            string dest = dirInfo.FullName + "\\" + Path.GetFileName(source);
                            File.Copy(source, dest);
                            pathOfSources.Add(dest);

                        }
                        s.AbsolutePathOfSources = pathOfSources;
                        s.AbsolutePathOfBinFolder = dirInfo2.FullName;
                        s.NameOfOutputBinary = "default.exe";
                        //s.AbsolutePathOfObjectsFiles = new List<string>();
                        solution = s;
                        if (Solution.CreateSolution(pathOfSolution, s))
                        {
                            this.Close();
                            createdSuccesfully = true;
                        }
                        else
                        {
                            MessageBox.Show("Failed To Create Solution! Application Will Close! Please Review The Log", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            Application.Exit(new CancelEventArgs());
                            createdSuccesfully = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        ExceptionPrinter.Print(ex);
                        MessageBox.Show("Something Went Clearly Wrong During The Project Creation Process. Everything Will Close! Please Review The Handy Log File In The Application's Directory. If It Is Not There--Please Contact Your Local Emergency Center!", "Oh Dear Something Went Wrong", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        this.Close();
                        createdSuccesfully = false;
                    }
                }
                else
                {
                    createdSuccesfully = false;
                    ExceptionPrinter.Print(new NothingToWorryAboutException("Project Already Exists In The Selected Project Directory Or No Sources Were Added!"));
                    MessageBox.Show("Error Project Already Exists With That Name! Please Retry With A Different Solution Name Or Try A Different Project Location! This Error Could Also Mean That You Have No Sources Added! Please Create One! If The Error Continues To Manifest Itself To The Program. It Might Be Time To Ask Someone Who Knows About These Stick Situations.", "Error In Creating Project!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else if (result == System.Windows.Forms.DialogResult.No)
            {             
                sourcesList.Clear();
                this.DialogResult = DialogResult.Retry;
            }
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void NewProjectDialog_Load(object sender, EventArgs e)
        {

        }

        private void sourcePanel_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
        }

        private void sourcePanel_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = e.Data.GetData(DataFormats.FileDrop) as string[];
            sourcesList.AddRange(files);
        }

       
    }
}
